﻿using System;
using System.Collections.Generic;
using System.Text;
using StockTrader.DAL.IDAL.HnxInfo.Oracle;
using StockTrader.BusinessService.DataContract.HnxInfo;
using System.Data.OleDb;
using System.Data;
using StockTrader.DAL.OracleHelper;
using StockTrader.DAL.IDAL.HnxInfo;

namespace StockTrader.DAL.Oracle.HnxInfo
{
    public class OHnxStock : IOHnxStock
    {
        private OleDbConnection _internalConnection;
        private OleDbTransaction _internalADOTransaction = null;

        public void Open(string connString)
        {
            if (_internalConnection == null)
                _internalConnection = new OleDbConnection(connString);
            if (_internalConnection.State != ConnectionState.Open)
                _internalConnection.Open();
        }
        public void Close()
        {
            if (_internalConnection != null && _internalConnection.State != System.Data.ConnectionState.Closed)
                _internalConnection.Close();
        }

        #region Parameters
        #endregion

        #region SQL string
        private const string GET_HNX_ALLSTOCKINFO = "SELECT  TRADING_DATE, TIME, STOCK_ID, CODE, TOTAL_LISTING_QTTY, TRADING_UNIT, LISTTING_STATUS, ADJUST_QTTY, REFERENCE_STATUS, ADJUST_RATE, DIVIDENT_RATE, STATUS, TOTAL_ROOM, CURRENT_ROOM, BASIC_PRICE, OPEN_PRICE,CLOSE_PRICE, CURRENT_PRICE, CURRENT_QTTY, HIGHEST_PRICE, LOWEST_PRICE, BEST_OFFER_PRICE, BEST_BID_PRICE, CEILING_PRICE, FLOOR_PRICE, TOTAL_OFFER_QTTY, TOTAL_BID_QTTY, BEST_OFFER_QTTY, BEST_BID_QTTY, PRIOR_PRICE, PRIOR_CLOSE_PRICE,MATCH_PRICE, MATCH_QTTY, DELETED, NAME, PARVALUE, MATCH_VALUE, FLOOR_CODE, IS_CALCINDEX, IS_DETERMINECL, DATE_NO, OFFER_COUNT, BID_COUNT, AVERAGE_PRICE, INDEX_PRICE, PREV_PRIOR_PRICE, YIELDMAT, PREV_PRIOR_CLOSE_PRICE, NM_TOTAL_TRADED_QTTY, NM_TOTAL_TRADED_VALUE, PT_MATCH_QTTY, PT_MATCH_PRICE, PT_TOTAL_TRADED_QTTY, PT_TOTAL_TRADED_VALUE, TOTAL_BUY_TRADING_QTTY, BUY_COUNT, TOTAL_BUY_TRADING_VALUE, TOTAL_SELL_TRADING_QTTY, SELL_COUNT, TOTAL_SELL_TRADING_VALUE, TOTAL_TRADING_QTTY, TOTAL_TRADING_VALUE, BUY_FOREIGN_QTTY, BUY_FOREIGN_VALUE, SELL_FOREIGN_QTTY, SELL_FOREIGN_VALUE, REMAIN_FOREIGN_QTTY FROM  STS_STOCKS_INFO WHERE FLOOR_CODE='02' AND STOCK_TYPE='2'";

        #endregion


        #region IOHnxStock Members


        public List<HnxStockData> getOHnxAllStockInfo()
        {
            List<HnxStockData> list = new List<HnxStockData>();
            try
            {
                OleDbDataReader rdr = OleDbHelper.ExecuteReaderNoParm(_internalConnection, _internalADOTransaction, CommandType.Text, GET_HNX_ALLSTOCKINFO);
                while (rdr.Read())
                {
                    list.Add(new HnxStockData(
                        Convert.ToDateTime(rdr["TRADING_DATE"]),
                        rdr["TIME"].ToString(),
                        Convert.ToDecimal(rdr["STOCK_ID"]),
                        rdr["CODE"].ToString(),
                        rdr["DATE_NO"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["DATE_NO"]),
                        rdr["TOTAL_LISTING_QTTY"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["TOTAL_LISTING_QTTY"]),
                        rdr["REFERENCE_STATUS"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["REFERENCE_STATUS"]),
                        rdr["STATUS"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["STATUS"]),
                        rdr["OPEN_PRICE"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["OPEN_PRICE"]),
                        rdr["CLOSE_PRICE"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["CLOSE_PRICE"]),
                        rdr["HIGHEST_PRICE"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["HIGHEST_PRICE"]),
                        rdr["LOWEST_PRICE"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["LOWEST_PRICE"]),
                        rdr["AVERAGE_PRICE"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["AVERAGE_PRICE"]),
                        rdr["BEST_OFFER_PRICE"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["BEST_OFFER_PRICE"]),
                        rdr["BEST_OFFER_QTTY"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["BEST_OFFER_QTTY"]),
                        rdr["TOTAL_OFFER_QTTY"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["TOTAL_OFFER_QTTY"]),
                        rdr["BEST_BID_PRICE"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["BEST_BID_PRICE"]),
                        rdr["BEST_BID_QTTY"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["BEST_BID_QTTY"]),
                        rdr["TOTAL_BID_QTTY"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["TOTAL_BID_QTTY"]),
                        rdr["PRIOR_CLOSE_PRICE"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["PRIOR_CLOSE_PRICE"]),
                        Convert.ToDecimal(rdr["MATCH_PRICE"]),
                        Convert.ToDecimal(rdr["MATCH_QTTY"]),
                        rdr["BID_COUNT"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["BID_COUNT"]),
                        rdr["OFFER_COUNT"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["OFFER_COUNT"]),

                        rdr["NM_TOTAL_TRADED_QTTY"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["NM_TOTAL_TRADED_QTTY"]),
                        rdr["NM_TOTAL_TRADED_VALUE"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["NM_TOTAL_TRADED_VALUE"]),
                        rdr["PT_MATCH_PRICE"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["PT_MATCH_PRICE"]),
                        rdr["PT_MATCH_QTTY"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["PT_MATCH_QTTY"]),
                        rdr["PT_TOTAL_TRADED_QTTY"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["PT_TOTAL_TRADED_QTTY"]),
                        rdr["PT_TOTAL_TRADED_VALUE"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["PT_TOTAL_TRADED_VALUE"]),
                        rdr["BUY_COUNT"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["BUY_COUNT"]),
                        rdr["SELL_COUNT"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["SELL_COUNT"]),
                        rdr["TOTAL_BUY_TRADING_QTTY"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["TOTAL_BUY_TRADING_QTTY"]),
                        rdr["TOTAL_BUY_TRADING_VALUE"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["TOTAL_BUY_TRADING_VALUE"]),
                        rdr["TOTAL_SELL_TRADING_QTTY"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["TOTAL_SELL_TRADING_QTTY"]),
                        rdr["TOTAL_SELL_TRADING_VALUE"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["TOTAL_SELL_TRADING_VALUE"]),
                        Convert.ToDecimal(rdr["BUY_FOREIGN_QTTY"]),
                        Convert.ToDecimal(rdr["BUY_FOREIGN_VALUE"]),
                        Convert.ToDecimal(rdr["SELL_FOREIGN_QTTY"]),
                        Convert.ToDecimal(rdr["SELL_FOREIGN_VALUE"]),
                        rdr["REMAIN_FOREIGN_QTTY"].Equals(DBNull.Value) ? 0 :
                                Convert.ToDecimal(rdr["REMAIN_FOREIGN_QTTY"]),
                        Convert.ToDecimal(rdr["CURRENT_ROOM"])));
                }
                rdr.Close();

                return list;
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public int countOHnxStockInfo()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
